package com.zhuzi.service;

import com.alibaba.fastjson2.JSON;
import com.google.common.collect.Lists;
import com.zhuzi.common.utils.BeanListUtils;
import com.zhuzi.component.KzzComponent;
import com.zhuzi.component.WeixinComponent;
import com.zhuzi.dto.jc.JcEntityDTO;
import com.zhuzi.dto.weixin.TextMD;
import com.zhuzi.dto.weixin.text.JcNotionStyle;
import com.zhuzi.entity.KzzAndStockEntity;
import com.zhuzi.utils.JcNewsGetUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.redisson.api.RBloomFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class JcNewsService {
    //巨潮-新闻-减持
    private static String PREFIX_MSG = "jc_new";

    @Autowired
    KzzComponent kzzComponent;
    @Resource
    WeixinComponent wixinComponent;
    @Resource
    private RBloomFilter rBloomFilter;


    /**
     * 巨潮新闻
     *
     * @param fetchKey  获取关键字
     * @param startDate 开始日期
     * @param endDate   结束日期
     * @param mdTitle   md标题
     * @param atAll     是否@所有人
     */
    public void jcNews(String fetchKey, LocalDate startDate, LocalDate endDate, String mdTitle, boolean atAll) {
        List<JcEntityDTO> fetchList = JcNewsGetUtils.fetch(fetchKey, startDate, endDate);
        filerKzzStock(fetchList);
        if (CollectionUtils.isEmpty(fetchList)) {
            log.info("巨潮-公告-[{}]-抓取为空", fetchKey);
            return;
        }
        //添加成功表示没有数据。可以发送消息|布隆过滤器过滤
        List<JcEntityDTO> fetNewList = fetchList.stream().filter(item -> rBloomFilter.add(PREFIX_MSG + item.getAnnId())).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(fetNewList)) {
            log.info("巨潮-公告-[{}]-所有消息都发送过,无需再次发送", fetchKey);
            return;
        }

        // 微信等机器人限制字符
        Lists.partition(fetchList, 20).stream().forEach(itemList -> {
            //微信消息
            List<JcNotionStyle.Item> jcList = BeanListUtils.copyList(itemList, JcNotionStyle.Item::new);

            JcNotionStyle jcNotionStyle = new JcNotionStyle().setTitle(mdTitle);
            jcNotionStyle.setItemList(jcList);
            TextMD.TextItem textItem = new TextMD.TextItem().setContent(jcNotionStyle.content());
            if (atAll) {
                textItem.all();
            }
            TextMD textMD = new TextMD().setText(textItem);
            wixinComponent.sendHint(JSON.toJSONString(textMD));
        });
    }

    /**
     * 过滤器kzz股票
     *
     * @param fetchList 被过滤列表
     * @return {@link List}<{@link JcEntityDTO}>
     */
    public void filerKzzStock(List<JcEntityDTO> fetchList) {
        List<String> stockList = kzzComponent.getAll().stream().map(KzzAndStockEntity::getStockCode).collect(Collectors.toList());
        List<JcEntityDTO> collect = fetchList.stream().filter(item -> stockList.contains(item.getSecCode())).collect(Collectors.toList());
        fetchList.clear();
        fetchList.addAll(collect);
    }

}
